<?php
/**
 * @author kysu
 * @copyright 2012
 */

include_once('DataProvider.class.php');

class CEOLinkModel extends DataProvider {

    /**
     * Contructor
     * @param $dbname : Database name
     * Default : mpay
     */
    public function __construct($dbname='') {
		if($dbname =='') {
			$this->dbname = 'face'; //Default: db sptonline.db.inc.php
		}
		else {
            $this->dbname = $dbname; //Manual: config file <classname>.db.inc.php in directory ./inc
        }
    }
	
	function updateUser($email, $password, $password_salt){
		if($email!='' && $password!='' && $password_salt!=''){
			$sql = "UPDATE phpfox_user SET password='".$password."', password_salt='".$password_salt."' WHERE email='".$email."'";
			$result = $this->executeNonQuery($sql);
			return $result;
		}
	}
	   
	public function insertUser($email, $password, $password_salt, $full_name, $joined, $last_ip_address) {
		$ip_address = $_SERVER['REMOTE_ADDR'];
		$time_stamp = time();
		
		//check exist user
		
		$quantity = 0;
		$sql = "SELECT count(*) FROM phpfox_user WHERE email = '".$email."' ";
		$quantity = $this->executeScalar($sql);
		
		if($quantity==0){
			$sql = "INSERT INTO phpfox_user(user_id,user_group_id,full_name,password,password_salt,email,gender,birthday,joined,last_ip_address) VALUES";
			$sql .= " (NULL, 2, '".$full_name."','".$password."','".$password_salt."','".$email."',1,'07161987',".$joined.",'".$last_ip_address."') ";
			$result = $this->executeQuery($sql);
						
			$sql = "SELECT max(user_id) FROM phpfox_user";
			$id = $this->executeScalar($sql);
			
			$sql = "UPDATE phpfox_user SET user_name='profile-".$id."' WHERE user_id=".$id;
			$result2 = $this->executeNonQuery($sql);
			
			$sql = "INSERT INTO phpfox_user_count(user_id) VALUES (".$id.")";
			$result3 = $this->executeQuery($sql);
			
			$sql = "INSERT INTO phpfox_user_field(user_id) VALUES (".$id.")";
			$result4 = $this->executeQuery($sql);
			
			$sql = "INSERT INTO phpfox_user_ip(ip_log,user_id,type_id,ip_address,time_stamp) VALUES (NULL,".$id.",'register','".$ip_address."',".$time_stamp.")";
			$resul5 = $this->executeQuery($sql);
			
			$sql = "INSERT INTO phpfox_user_space (user_id) VALUES (".$id.")";
			$resul6 = $this->executeQuery($sql);
			
			$sql = "INSERT INTO phpfox_user_activity (user_id) VALUES (".$id.")";
			$resul7 = $this->executeQuery($sql);
			
			return $result;
		}
    }
		
}
?>